package com.atguigu.gmall.mq.receiver;

import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class ConfirmReceiver {

    @Autowired
    private RedisTemplate redisTemplate;
    //使用注解监听  将交换机与队列都会初始化
    @SneakyThrows  //这个注解自动接受异常
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue.confirm",durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = "exchange.confirm"),
            key = {"routing.confirm"}
    ))
    public void getMsg(String msg, Message message, Channel channel){

        try {
            System.out.println("接受的消息：\t"+msg);
            System.out.println("接受的消息message：\t"+new String(message.getBody()));
        } catch (Exception e) {
            //  第一个参数表示消息标签：第二参数表示是否是批量签收  true: 批量签收， false: 单条签收  第三个参数表示：是否重回队列 true:重回，false:不重回
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            e.printStackTrace();
        }

        //确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    //监听延迟消息
    @SneakyThrows
    @RabbitListener(queues = DeadLetterMqConfig.queue_dead_2)
    public void getMsg2(String msg, Message message, Channel channel){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("接收时间:"+simpleDateFormat.format(new Date())+msg);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    //监听延迟消息 基于插件
    @SneakyThrows
    @RabbitListener(queues = DelayedMqConfig.queue_delay_1)
    public void getMsg3(String msg, Message message, Channel channel){
        Boolean result = this.redisTemplate.opsForValue().setIfAbsent("lock", "0", 1, TimeUnit.MINUTES);
        if (!result){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("接收时间:"+simpleDateFormat.format(new Date())+msg);
        } catch (Exception e) {
            //如果发生异常直接删除
            this.redisTemplate.delete("lock");
            e.printStackTrace();
        }
        //确认签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
